﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FTI
{
    public static partial class Extensions
    {
        public static bool IsNullOrEmpty(this string pThis)
        {
            return String.IsNullOrEmpty(pThis) || String.IsNullOrWhiteSpace(pThis);
        }

        public static bool IsNotNullOrEmpty(this string pThis)
        {
            return !String.IsNullOrEmpty(pThis.IsNotNull("", X => X.Trim()));
        }

        public static bool Eq(this string pThis, string right)
        {
            return pThis.Equals(right, StringComparison.OrdinalIgnoreCase);
        }

        public static bool IsNullOrWhiteSpace(this string oObject)
        {
            if (oObject == null) return true;

            for (int i = 0; i < oObject.Length; i++)
            {
                if (!Char.IsWhiteSpace(oObject[i])) return false;
            }

            return true;
        }

        public static int ToInteger<S>(this S source)
        {
            int result = 0;

            if (source is DBNull)
            {
                return 0;
            }
            else if (source is string && Int32.TryParse((source).ToString(), out result))
            {
                return result;
            }
            else if (source is int)
            {
                return Convert.ToInt32(source);
            }
            else if (source is string && Int32.TryParse(source.ToString(), out result))
            {
                return result;
            }
            else if (source != null && !(source is string))
            {
                return Convert.ToInt32(source);
            }
            else return 0;
        }

        public static NameValueCollection ToNameValueCollection(this string str)
        {
            NameValueCollection oCollection = new NameValueCollection();

            foreach (string pair in str.Split('&'))
            {
                string[] value = pair.Split('=');

                oCollection.Add(value[0], value[1]);
            }

            return oCollection;
        }

        #region String Builder Extensions

        public static StringBuilder IfAppend(this StringBuilder source, bool comparision, object value)
        {
            if (comparision)
                source.Append(value);
            return source;
        }

        #endregion

        private static string CarriageReturn
        {
            get
            {
                return new string(new char[] { (char)13, (char)10 });
            }
        }

        private static string NewLine
        {
            get { return new string(new char[] { (char)10 }); }
        }

        public static String ToCSV(this DataTable dt, bool header, bool quoteall)
        {
            #region Documentation
            /*  Author:     Kenneth Carter
             *  Date:       10/5/2011
             *  Article:    http://creativyst.com/Doc/Articles/CSV/CSV01.htm
             */
            #endregion

            using (StringWriter oWriter = new StringWriter())
            {
                Utility.WriteToCSVStream(oWriter, dt, header, quoteall);
                return oWriter.ToString();
            }
        }

        public static string ToProperCase(this string pSrc, bool bProperCaseEachWord)
        {
            bool bToUpper = true;

            char[] @string = pSrc.Trim().ToCharArray();

            for (int i = 0; i < @string.Length; i++)
            {
                string @char = @string[i].ToString();

                bToUpper = bToUpper && @char.Equals(@char.ToLower(), StringComparison.CurrentCulture);

                if (@char == " ")
                {
                    bToUpper = bProperCaseEachWord;
                    continue;
                }
                else
                {
                    if (bToUpper)
                        @string[i] = Convert.ToChar(@char.ToUpper());
                    bToUpper = false;
                }
            }

            return new string(@string);
        }

        public static string AddSpacesToSentence(this string text, bool preserveAcronyms)
        {
            if (text.IsNullOrWhiteSpace() || String.IsNullOrEmpty(text))
                return String.Empty;
            StringBuilder oResult = new StringBuilder(text.Length * 2);
            oResult.Append(text[0]);
            for (int i = 1; i < text.Length; i++)
            {
                if (char.IsUpper(text[i]))
                {
                    if (((text[i - 1] != ' ' && text[i - 1] != '_' && text[i - 1] != '/') && !char.IsUpper(text[i - 1])) ||
                        (preserveAcronyms && char.IsUpper(text[i - 1]) &&
                         i < text.Length - 1 && !char.IsUpper(text[i - 1])))
                        oResult.Append(' ');
                }
                oResult.Append(text[i]);
            }
            return oResult.ToString().Trim();
        }
    }
}
